﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TAM_SPORT
{
    public class CheckLicense
    {
        private static string[] MONTH = { "M", "N", "T", "A", "F", "U", "W", "Y", "I", "V"  };
        private static string[] DAY = { "D", "A", "Y", "5", "4", "3", "G", "N", "9", "V" };
        private static string[] TEMP = { "T", "E", "H", "N", "7", "1", "Y", "Z", "9", "V" };
        private static string[] TEMP1 = { "Y", "Z", "U", "J", "T", "K", "0", "Z", "9", "V" };
        private static int getNumber()
        {
            Random rnd = new Random();
            return rnd.Next(1, 10);
        }
        public static bool checkE(string strE)
        {
            if(strE == null || strE.Trim().Length <=2)
            {
                return false;
            }

            if (!checkContains(TEMP1, strE.Substring(0, 1)))
            {
                return false;
            }
            string strToday = (DateTime.Now.Day + DateTime.Now.DayOfYear).ToString();
            if(!strToday.Equals(strE.Substring(1)))
            {
                 return false;
            }
            return true;
        }
        public static bool checkA(string strA)
        {
            bool flg = true;
            string preNext = "";
            string next = "";
            if (strA.StartsWith("A"))
            {
                preNext = strA.Substring(1, 2);
                next = strA.Substring(3,1);
                if (!"14".Equals(preNext) || !"M".Equals(next))
                {
                    flg = false;
                }
            }
            else if (strA.StartsWith("B"))
            {
                preNext = strA.Substring(1, 2);
                next = strA.Substring(3,1);
                if (!"15".Equals(preNext) || !"N".Equals(next))
                {
                    flg = false;
                }
            }
            else if (strA.StartsWith("C"))
            {
                preNext = strA.Substring(1, 2);
                next = strA.Substring(3,1);
                if (!"16".Equals(preNext) || !"A".Equals(next))
                {
                    flg = false;
                }
            }
            else if (strA.StartsWith("D"))
            {
                preNext = strA.Substring(1, 2);
                next = strA.Substring(3,1);
                if (!"17".Equals(preNext) || !"F".Equals(next))
                {
                    flg = false;
                }
            }
            else if (strA.StartsWith("E"))
            {
                preNext = strA.Substring(1, 2);
                next = strA.Substring(3,1);
                if (!"18".Equals(preNext) || !"Q".Equals(next))
                {
                    flg = false;
                }
            }
            else
            {
                flg = false;
            }
            return flg;
        }

        public static bool checkB(string strB)
        {
            bool flg = true;
            string preNext = "";
            string next = "";
            int month = 0;
            int monthtemp = 0;
            
            if(!checkContains(MONTH, strB.Substring(0,1)))
            {
                return false;
            }

            preNext = strB.Substring(1, 2);
            next = strB.Substring(3,2);
            if (int.TryParse(preNext, out month) && int.TryParse(next, out monthtemp))
            {
                if (month <= 0 || monthtemp <= 0 || month > 5 || monthtemp > 12)
                {
                    flg = false;
                }
                else
                {
                    switch (month)
                    {
                        case 1:
                            if (monthtemp > 3)
                            {
                                flg = false;
                            }
                            break;
                        case 2:
                            if (monthtemp > 6 || monthtemp < 4)
                            {
                                flg = false;
                            }
                            break;
                        case 3:
                            if (monthtemp > 9 || monthtemp < 7)
                            {
                                flg = false;
                            }
                            break;
                        case 4:
                            if (monthtemp > 11 || monthtemp < 10)
                            {
                                flg = false;
                            }
                            break;
                        case 5:
                            if (monthtemp != 12)
                            {
                                flg = false;
                            }
                            break;
                    }
                }
            }
            else
            {
                flg = false;
            }
            
            return flg;
        }

        public static bool checkContains(string[] stringArray, string value)
        {
            int pos = Array.IndexOf(stringArray, value);
            if (pos > -1)
            {
                return true;
            }
            return false;
        }
        public static bool checkDate(string strDate)
        {
            DateTime dt;
            if (DateTime.TryParseExact(strDate, "yyyyMMdd", null, System.Globalization.DateTimeStyles.None, out dt))
            {
                if (DateTime.Now.CompareTo(dt) > 0)
                {
                    return false;
                }
            }else
            {
                return false;
            }
            return true;
        }

        public static DateTime convertString2Date(string strDate)
        {
            DateTime dt;
            DateTime.TryParseExact(strDate, "yyyyMMdd", null, System.Globalization.DateTimeStyles.None, out dt);
            return dt;
        }
        public static bool checkC(string strB)
        {
            bool flg = true;
            string preNext = "";
            string next = "";
            int month = 0;
            int monthtemp = 0;
            if (!checkContains(DAY, strB.Substring(0, 1)))
            {
                return false;
            }
            preNext = strB.Substring(1, 2);
            next = strB.Substring(3,2);
            if (int.TryParse(preNext, out month) && int.TryParse(next, out monthtemp))
            {
                if (month <= 0 || monthtemp <= 0 || month > 2 || monthtemp > 31)
                {
                    flg = false;
                }
                else
                {
                    switch (month)
                    {
                        case 1:
                            if (monthtemp > 15)
                            {
                                flg = false;
                            }
                            break;
                        case 2:
                            if (monthtemp > 31 || monthtemp < 16)
                            {
                                flg = false;
                            }
                            break;
                    }
                }
            }
            else
            {
                flg = false;
            }
            
            return flg;
        }

        public static bool checkD(string strD)
        {
            if (!checkContains(TEMP, strD.Substring(0, 1)))
            {
                return false;
            }
            string preNext = "";
            string next = "";
            string add = "";
            int month = 0;
            int monthtemp = 0;
            int addNum = 0;
            preNext = strD.Substring(1, 1);
            next = strD.Substring(2, 1);
            add = strD.Substring(3, 1);
            if (int.TryParse(preNext, out month)
                && int.TryParse(next, out monthtemp)
                && int.TryParse(add, out addNum))
            {
                if ((month + monthtemp + addNum) != 20)
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
            return true;
        }
    }
}
